#include <algorithm>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        if(envelopes.empty())return 0;
        sort(envelopes.begin(),envelopes.end(),[](const auto &c1,const auto&c2){
            return c1[0]<c2[0]||(c1[0]==c2[0] &&c1[1]>c2[1]);
        });
        vector<int>dp(envelopes.size()+1,1);
        int len=0;
        for(int i=0;i<envelopes.size();i++)
        {
            int l=0,r=len;
            while(l<r)
            {
                int mid=l+r+1>>1;
                if(dp[mid]<envelopes[i][1])l=mid;
                else r=mid-1;
            }
            len=max(len,r+1);
            dp[r+1]=envelopes[i][1];
        }

        return len;
    }
};